package cn.com.cifi.mars.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;


/**
 * 
 * @Description：MQ消息 配置
 * @author：yyd
 * @date：2020年6月22日 上午10:17:46
 */
@Component
public class OaMqUtils {

    @Value("${oa_queue}")
    private String myQueue;
    
    @Value("${oa.activemq.url}")
    private String brokerUrl;

    @Bean
    public Queue queue() {
        return new ActiveMQQueue(myQueue);
    }

    /**
     * oa连接工厂
     * @return
     */
    @Bean(name = "secondConnectionFactory")
    public ConnectionFactory secondConnectionFactory() {
    	return new ActiveMQConnectionFactory(brokerUrl);
    }
    
    /**
     * oa的jms模板
     * @param connectionFactory
     * @return
     */
    @Bean(name = "secondTemplate")
    public JmsMessagingTemplate secondRabbitTemplate(
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
    	return new JmsMessagingTemplate(connectionFactory);
    }
    
    /**
     * 监听oa的jms容器工厂
     * @param connectionFactory
     * @param configurer
     * @return
     */
    @Bean(name = "oaFactory")
    public JmsListenerContainerFactory jmsListenerContainerFactoryOa(
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory,
            DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory containerFactory = new DefaultJmsListenerContainerFactory();
        configurer.configure(containerFactory, connectionFactory);
        return containerFactory;
    }
    
  
}
